// 1、 引入crypto-js
// let CryptoJS = require('crypto-js') // 或者import CryptoJS from 'crypto-js'
import CryptoJS from 'crypto-js'
// 2、Crypto加密方法封装
const aesUtil = {
  // 加密函數
  encrypt: (word, keyStr) => {
    if (word instanceof Object) {
      //JSON.stringify
      word = JSON.stringify(word)
    }
    let key = CryptoJS.enc.Utf8.parse(keyStr)
    let encryptedObj = CryptoJS.AES.encrypt(
      CryptoJS.enc.Utf8.parse(word),
      key,
      {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
      }
    )
    return encryptedObj.toString()
  },
  // 解密函數
  decrypt: (word, keyStr) => {
    let key = CryptoJS.enc.Utf8.parse(keyStr)
    let decrypt = CryptoJS.AES.decrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.ZeroPadding,
    })
    let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString()
    return decString
  },
}

export { aesUtil }
